package com.huanwei.cc.publics.kd_khd.tcp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.czy1121.view.RoundButton;
import com.huanwei.cc.R;
import com.huanwei.cc.publics.activity.ZuoYeGuiJi_10_Activity;
import com.huanwei.cc.publics.kd_khd.MyEvent;
import com.huanwei.cc.publics.kd_khd.utils.JSONUtils;
import com.huanwei.cc.publics.kd_khd.utils.NetUtil;
import com.huanwei.cc.publics.kd_khd.utils.SpfUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 * 现场拍摄记录
 */
public class Locale_Activity extends AppCompatActivity {

    Client client;

    @BindView(R.id.bt_dj)
    RoundButton bt1;

    @BindView(R.id.bt_dj2)
    RoundButton btDj2;

    @BindView(R.id.textView7)
    EditText textView7;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.textView11)
    TextView textView11;
    @BindView(R.id.sys)
    RoundButton sys;
    @BindView(R.id.wendu)
    TextView wendu;
    @BindView(R.id.shidu)
    TextView shidu;
    String shuju2,shuju;

    @SuppressLint("HandlerLeak")
    private Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    break;
                case 2:
                    Log.e("999", "连接失败！");
                    Toast.makeText(Locale_Activity.this, "连接失败，服务器端有问题！！!", Toast.LENGTH_SHORT).show();
                    bt1.setText("开始连接");

                    cons.isConnecting = true;
                    break;
                case 3:
                    Toast.makeText(Locale_Activity.this, "断开连接！", Toast.LENGTH_SHORT).show();
                    Log.e("888", "断开连接！");
                    bt1.setText("开始连接");
                    cons.isConnecting = true;

                    break;
                case 4:
                    Toast.makeText(Locale_Activity.this, "服务器连接成功！", Toast.LENGTH_SHORT).show();
                    Log.e("888", "服务器连接成功！");
                    cons.isConnecting = false;
                    bt1.setText("断开连接");
                    startActivity(new Intent(Locale_Activity.this, ZuoYeGuiJi_10_Activity.class));

                    break;
                case 5:
                    String ss = (String) msg.obj;
                    Toast.makeText(Locale_Activity.this, ""+ss, Toast.LENGTH_SHORT).show();
                    Log.e("888", ss);
                    MyEvent myEvent=new MyEvent();
                    myEvent.setContent("dizhi");
                    myEvent.setSc(ss);
                    EventBus.getDefault().post(myEvent);



                    break;
                default:
                    break;
            }

        }
    };


    List<String> strings;
    StringBuilder ST;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locale);

        strings = new ArrayList<>();
        ButterKnife.bind(this);
        ST = new StringBuilder();

    }



    // 取消注册
    @Override
    public void onDestroy() {
        super.onDestroy();

        cons.isConnecting = true;
        if (client != null) {
            client.gb();
        }

    }

    @OnClick({R.id.sys, R.id.bt_dj2, R.id.bt_dj, R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_dj2:
                if (cons.isConnecting) {
                    Toast.makeText(Locale_Activity.this, "请检查网络!", Toast.LENGTH_SHORT).show();
                    bt1.setText("开始连接");
                } else {
                    //发送数据
                    String s = textView7.getText().toString();
                    if (TextUtils.isEmpty(s)) {
                        Toast.makeText(this, "请输入数据！", Toast.LENGTH_SHORT).show();
                        break;

                    }

                    kaishifasong(s);
                }


                break;
            case R.id.img_back:

                finish();

                break;


            case R.id.sys:

                Intent intent = new Intent(this, SZIP_Activity.class);
                startActivityForResult(intent, 999);

                break;

            case R.id.bt_dj:
                if (cons.isConnecting) {
                    final String duankou = (String) SpfUtils.get(Locale_Activity.this, "duankou", "8080");

                    if (NetUtil.isNetworkAvailable(Locale_Activity.this)) {

                        new AsyncTask<Void, Void, Void>() {
                            @Override
                            protected Void doInBackground(Void... params) {
                                client = new Client((String) SpfUtils.get(Locale_Activity.this, "ip", "192.168.43.1"), Integer.parseInt(duankou), h);
                                return null;
                            }
                        }.execute();

                    } else {
                        Toast.makeText(Locale_Activity.this, "请检查网络!", Toast.LENGTH_SHORT).show();
                        bt1.setText("开始连接");
                    }


                } else {
                    bt1.setText("开始连接");
                    client.gb();
                    cons.isConnecting = true;

                }
                break;
        }
    }


    private void kaishifasong(String s) {
        client.fasong(s);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 999) {
            Bundle b = data.getExtras(); //data为B中回传的Intent
            String str = b.getString("ewm");//str即为回传的值
            if (!TextUtils.isEmpty(str)) {
                strings.add(str);

            }

        }
    }
}
